﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Transactions;
using System.Web.Http;
using yiCommerce.Service.Orders;
using yiCommerce.Service.Shippings;

namespace yiCommerce.Areas.Admin.Controllers.api
{
    public class OrderShipResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
        public int Id { get; set; }
    }

    public class OrderShipRequest
    {
        public int OrderId { get; set; }
        public string TrackingNo { get; set; }
    }

    public class ShippingApiController : ApiController
    {

        private IShippingService _shippingService;
        private IOrderProcessingService _orderProcessingService;

        public ShippingApiController(IShippingService shippingService, IOrderProcessingService orderProcessingService)
        {
            _shippingService = shippingService;
            _orderProcessingService = orderProcessingService;
        }

        public OrderShipResult SetOrderShipping(OrderShipRequest request)
        {
            var result = new OrderShipResult();
            try
            {
                using (var trans = new TransactionScope())
                {

                    _shippingService.SetShipped(request.OrderId, request.TrackingNo);
                    _orderProcessingService.MarkOrderShipped(request.OrderId);
                    trans.Complete();
                    result.Status = "0";

                }
            }
            catch (Exception e)
            {
                
                result.Status = "1";
                result.Message =e.Message; 
            }

            return result;
        }
    }
}
